/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package com.bullionxpress.server.application;

import java.rmi.*;

import java.rmi.registry.*;


import com.bullionxpress.server.impl.AccountRegistrar;
import com.bullionxpress.server.impl.AdminManager;
import com.bullionxpress.server.impl.ChartProducer;
import com.bullionxpress.server.impl.ChartRecorder;
import com.bullionxpress.server.impl.InstrumentManager;
import com.bullionxpress.server.impl.OrderExecutor;
import com.bullionxpress.server.impl.TickManager;
import com.bullionxpress.server.impl.OrderManager;
import com.bullionxpress.server.impl.PositionManager;
import com.bullionxpress.server.impl.QuoteManager;
import com.bullionxpress.server.impl.TransactionManager;
import com.bullionxpress.server.robo.MarketMaker;
import com.bullionxpress.server.impl.UserManager;
import com.bullionxpress.server.impl.UserRegistrar;
import com.bullionxpress.server.robo.JadaPriceUpdater;
import com.bullionxpress.shared.service.UserDatabase;
import com.bullionxpress.shared.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

public class Server {

    int port;
    String address;
    Registry registry;    // rmi registry for lookup the remote objects.

    public static String getProperty(String property) {
        try {
            Properties p = new Properties();
            p.load(new FileInputStream(new File("./server.properties")));
            return p.getProperty(property);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "<NA>";
    }

    public Server() {
        // get the address of this host.
        address = "localhost";//getProperty("address");
        //String strPort = getProperty("port");

        port = 8888;  // this port(registry’s port)
        Log.info("address=" + address + ",port=" + port);
        try {
            
            // create the registry and bind the name and object.
            registry = LocateRegistry.createRegistry(port);
            AccountRegistrar ar = new AccountRegistrar();
            PositionManager pm = new PositionManager();
            InstrumentManager im = new InstrumentManager();
            OrderManager om = new OrderManager();
            QuoteManager qm = new QuoteManager();
            UserManager um = new UserManager();
            AdminManager am = new AdminManager();
            UserDatabase udb = new UserRegistrar();
            TransactionManager trm = new TransactionManager();
            TickManager tm = new TickManager();
            ChartRecorder recorder = new ChartRecorder();
            MarketMaker maker = new MarketMaker();
            ChartProducer producer = new ChartProducer();
            producer.addChartListener(recorder);

            ServerGlobals.setOrderManager(om);
            ServerGlobals.setPositionManager(pm);
            ServerGlobals.setQuoteManager(qm);
            ServerGlobals.setTickManager(tm);

            // External Services.
            OrderExecutor executor = new OrderExecutor();
            JadaPriceUpdater updater = new JadaPriceUpdater();
            Log.info("Executor:" + executor.toString());

            registry.rebind("TransactionDatabase", trm);
            registry.rebind("AccountDatabase", ar);
            registry.rebind("UserDatabase", udb);
            registry.rebind("InstrumentDatabase", im);
            registry.rebind("OrderService", om);
            registry.rebind("ChartService", producer);
            registry.rebind("LoginService", um);
            registry.rebind("AdminService", am);
            registry.rebind("ForgotPasswordService", um);
            registry.rebind("PositionService", pm);
            registry.rebind("ChangePasswordService", um);
            registry.rebind("QuoteService", qm);
            registry.rebind("TransactionService", om);
            registry.rebind("MarketService", om);

            Log.info("Bound all services");

        } catch (RemoteException e) {
            Log.info("Exception occured:" + e);
        }
    }
}
